package org.linlinjava.litemall.web.service;

import java.math.BigDecimal;
import java.util.List;

import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.core.util.ResponseUtil.Response;
import org.linlinjava.litemall.db.domain.LitemallSettlement;
import org.linlinjava.litemall.db.domain.LitemallSettlementLog;
import org.linlinjava.litemall.db.domain.LitemallUser;
import org.linlinjava.litemall.db.service.LitemallSettlementLogService;
import org.linlinjava.litemall.db.service.LitemallSettlementService;
import org.linlinjava.litemall.db.service.LitemallUserService;
import org.linlinjava.litemall.web.util.BeanConvertUtil;
import org.linlinjava.litemall.web.vo.AuthKnockOffRequest;
import org.linlinjava.litemall.web.vo.CalculationOrderVO;
import org.linlinjava.litemall.web.vo.OrderCalculateSettlementResponseVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author ：stephen
 * @date ：Created in 2020/2/21 10:42
 * @description：TODO
 */
@Service
public class WebSettlementLogService {

    @Autowired
    private LitemallUserService userService;
    @Autowired
    private LitemallSettlementService settlementService;
    @Autowired
    private WebOrderService orderService;
    @Autowired
    private LitemallSettlementLogService settlementLogService;

    /**
     * 添加ipad用户结算记录
     * @param userId
     * @param calculationOrderVos
     * @return
     */
    @Transactional
    public Response<Object> add(Integer shopId, Integer userId, AuthKnockOffRequest request){
    	LitemallUser user = userService.findById(userId);
    	
    	//新增交接记录
    	LitemallSettlement litemallSettlement = new LitemallSettlement();
    	litemallSettlement.setShopId(shopId);
    	litemallSettlement.setUserId(userId);
    	litemallSettlement.setUserName(user.getUsername());
    	String deviceNo = request.getDeviceNo();
    	litemallSettlement.setDeviceNo(deviceNo);
    	OrderCalculateSettlementResponseVO response = orderService.calculateSettlement(userId, deviceNo);
    	litemallSettlement.setOrderAmount(response.getOrderAmount());
    	litemallSettlement.setRefundAmount(response.getRefundAmount());
    	settlementService.add(litemallSettlement);
    	
    	//新增交接详情
    	Integer settlementId = litemallSettlement.getId();
    	BigDecimal actualAmount = BigDecimal.ZERO;
    	List<CalculationOrderVO> calculationOrderVos = request.getCalculationOrderVos();
        for (CalculationOrderVO calculationOrderVo : calculationOrderVos) {
        	LitemallSettlementLog log = BeanConvertUtil.toSettlementLot(user, calculationOrderVo);
        	log.setSettlementId(settlementId);
        	settlementLogService.add(log);
        	actualAmount =  actualAmount.add(calculationOrderVo.getAmount().multiply(calculationOrderVo.getNumber()));
		}
        
        litemallSettlement.setActualAmount(actualAmount);
        settlementService.update(litemallSettlement);
        
        return ResponseUtil.ok();
    }
}
